/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   SpriteConfig.cs
 * CreateData   :   2023/7/9 14:49:17
 * UnityVersion :   2021.3.20f1c1
 * Description  :   精灵配置
************************************************/

#if UNITY_EDITOR
using UnityEditor;
#endif

using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;

namespace Main
{
    public class SpriteConfig : ScriptableObjectConfigBase<SpriteInfo>
    {
#if UNITY_EDITOR
        [Title("Body")]
        [SerializeField, Range(1, 20), LabelText("GruopID")] private int Gruop = 1;
#endif
#if UNITY_EDITOR
        /// <summary>
        /// 生成 ID
        /// </summary>
        private string SpawnID
        {
            get
            {
                const int Length = 10000;
                for (int i = 0; i < Length; i++)
                {
                    string id = (Gruop * Length + i).ToString();
                    if (!datas.ContainsKey(id))
                    {
                        return id;
                    }
                }
                throw new IndexOutOfRangeException("超出下标范围");
            }
        }
        [OnInspectorGUI]
        private void OnInspectorGUI()
        {
            Event currentEvent = Event.current;
            Rect dropArea = GUILayoutUtility.GetRect(0f, 0f, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true));

            switch (currentEvent.type)
            {
                case EventType.DragUpdated:
                case EventType.DragPerform:
                    if (!dropArea.Contains(currentEvent.mousePosition))
                        return;

                    if (DragAndDrop.paths.Any(path => IsSpritePath(path)))
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                        DragAndDrop.AcceptDrag();
                        if (currentEvent.type == EventType.DragPerform)
                        {
                            foreach (var draggedObject in DragAndDrop.objectReferences)
                            {
                                string path = AssetDatabase.GetAssetPath(draggedObject);
                                Sprite sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
                                if (sprite)
                                {
                                    if (!datas.Values.Any(item => item.sprite == sprite))
                                    {
                                        datas.Add(SpawnID, new SpriteInfo() { sprite = sprite, description = draggedObject.name });
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        }
        private bool IsSpritePath(string path)
        {
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            if (textureImporter != null && textureImporter.textureType == TextureImporterType.Sprite)
            {
                return true;
            }
            return false;
        }

        [Button("添加"), HorizontalGroup("Button")]
        private void Add()
        {
            datas.Add(SpawnID, new SpriteInfo());
        }
        [Button("清空Null"), HorizontalGroup("Button")]
        private void ClearNull()
        {
            List<string> recoveryID = new List<string>();
            foreach (var item in datas)
            {
                if (item.Value.sprite == null)
                {
                    recoveryID.Add(item.Key);
                }
            }
            foreach (var item in recoveryID)
            {
                datas.Remove(item);
            }
        }
        [Button("清空"), HorizontalGroup("Button")]
        private void Clear()
        {
            datas.Clear();
        }
#endif
    }
    public struct SpriteInfo
    {
#if UNITY_EDITOR
        private void SpriteChange()
        {
            if (sprite)
            {
                description = sprite.name;
            }
            else
            {
                description = "";
            }
        }
        [HorizontalGroup(Width = 40), HideLabel,
        PreviewField(ObjectFieldAlignment.Center, Height = 40), OnValueChanged("SpriteChange"), AssetsOnly]
#endif
        public Sprite sprite;
        [HorizontalGroup, HideLabel]
        public string description;
    }
}
